#include "menutobooks.h"
#include "ui_menutobooks.h"
#include <vector>
#include <QMessageBox>


MenuToBooks::MenuToBooks(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MenuToBooks)
{
    ui->setupUi(this);
    page = 0;//当前页数
    pagesize = 0;//总页数
    page_size = 3;//一页放几本书
}

MenuToBooks::~MenuToBooks()
{
    delete ui;
}

void MenuToBooks::on_pushButton_clicked()
{
    //借书1
    int i = page_size*page;
    int borrowing = borrow(stu->ID,bookList[i]->ID);
    if(borrowing == 0)
    {
        QMessageBox::warning(this,"提示","借书成功！");
        bookList = search_multiplebookReturn("notFuzzySearch","menu_id",menu->ID,bookDatPath);
        //this->clear();
        this->show(bookList,page);
        return;
     }
    else if(borrowing== -3)
    {
        QMessageBox::warning(this,"提示","该书已被预约，请选择其他书籍！！");
        return;

    }
    else if(borrowing== 1)
    {
        QMessageBox::warning(this,"提示","借书成功，恭喜被加入英雄榜！");
        bookList = search_multiplebookReturn("notFuzzySearch","menu_id",menu->ID,bookDatPath);
        //this->clear();
        this->show(bookList,page);
        return;
    }
     else if(borrowing== -1)
     {
         QMessageBox::warning(this,"提示","您借书已超上限！");
         return;
     }
     else if(borrowing == -2)
     {
         QMessageBox::warning(this,"提示","该书已被借走，请选择其他书籍！");
         return;
     }

}

void MenuToBooks::on_pushButton_2_clicked()
{
    //借书2
    int i = page_size*page+1;
    int borrowing = borrow(stu->ID,bookList[i]->ID);
    if(borrowing == 0)
    {
        QMessageBox::warning(this,"提示","借书成功！");
        bookList = search_multiplebookReturn("notFuzzySearch","menu_id",menu->ID,bookDatPath);
        //this->clear();
        this->show(bookList,page);
        return;
     }
    else if(borrowing== -3)
    {
        QMessageBox::warning(this,"提示","该书已被预约，请选择其他书籍！！");
        return;

    }
    else if(borrowing== 1)
    {
        QMessageBox::warning(this,"提示","借书成功，恭喜被加入英雄榜！");
        bookList = search_multiplebookReturn("notFuzzySearch","menu_id",menu->ID,bookDatPath);
        //this->clear();
        this->show(bookList,page);
        return;
    }
     else if(borrowing== -1)
     {
         QMessageBox::warning(this,"提示","您借书已超上限！");
         return;
     }
     else if(borrowing == -2)
     {
         QMessageBox::warning(this,"提示","该书已被借走，请选择其他书籍！");
         return;
     }
}

void MenuToBooks::on_pushButton_3_clicked()
{
    //借书3
    int i = page_size*page+2;
    int borrowing = borrow(stu->ID,bookList[i]->ID);
    if(borrowing == 0)
    {
        QMessageBox::warning(this,"提示","借书成功！");
        bookList = search_multiplebookReturn("notFuzzySearch","menu_id",menu->ID,bookDatPath);
        //this->clear();
        this->show(bookList,page);
        return;
     }
    else if(borrowing== -3)
    {
        QMessageBox::warning(this,"提示","该书已被预约，请选择其他书籍！！");
        return;

    }
    else if(borrowing== 1)
    {
        QMessageBox::warning(this,"提示","借书成功，恭喜被加入英雄榜！");
        bookList = search_multiplebookReturn("notFuzzySearch","menu_id",menu->ID,bookDatPath);
        //this->clear();
        this->show(bookList,page);
        return;
    }
     else if(borrowing== -1)
     {
         QMessageBox::warning(this,"提示","您借书已超上限！");
         return;
     }
     else if(borrowing == -2)
     {
         QMessageBox::warning(this,"提示","该书已被借走，请选择其他书籍！");
         return;
     }
}

void MenuToBooks::getData(Menu*menu,Student*stu)
{
    this->menu=menu;
    this->stu=stu;
     //用menu的ID找到一个book的list
    bookList = search_multiplebookReturn("notFuzzySearch","menu_id",menu->ID,bookDatPath);
    QString key = QString(QString::fromLocal8Bit(menu->name));
    ui->label_5->setText(key);
    key = QString(QString::fromLocal8Bit(menu->ISBN));
    ui->label_6->setText(key);
    key = QString(QString::fromLocal8Bit(menu->writer));
    ui->label_7->setText(key);
    key = QString(QString::fromLocal8Bit(menu->publish));
    ui->label_8->setText(key);
    show(bookList,0);
}

void MenuToBooks::showPageButton()
{
    ui->pushButton_4->show();
    ui->pushButton_5->show();
    ui->pushButton_6->show();
    ui->lineEdit->show();
    ui->label_25->show();
}

void MenuToBooks::show(vector<Book*>booklist,int numofpage)
{
    pagesize = (booklist.size()-1)/page_size;
    int i = numofpage*page_size;
    QString key;
    this->clear();
    if(numofpage<=pagesize&&numofpage>=0)
    {
        if(booklist.size()>i)
        {
            this->show1();
            this->showPageButton();
            key = QString(QString::number(i+1));
            ui->label_13->setText(key);
            string s1 = "正常",s2 = "破旧",s3 = "破损（需更换）";
            string s4= "信息错误",s5="未借出",s6="已借出";
            int book_cond = booklist[i]->book_cond;
            if(book_cond==0)
            {
                key = str2Qstr(UtfToGbk(s1.data()));
            }
            else if(book_cond==1)
            {
                key = str2Qstr(UtfToGbk(s2.data()));
            }
            else if(book_cond==2)
            {
                key = str2Qstr(UtfToGbk(s3.data()));
            }
            else
            {
                key = str2Qstr(UtfToGbk(s4.data()));
            }
            ui->label_14->setText(key);
            int condition = booklist[i]->isBorrowed;
            if(condition==-1)
            {
                key = str2Qstr(UtfToGbk(s5.data()));
            }
            else
            {
                key = str2Qstr(UtfToGbk(s6.data()));
            }
            ui->label_15->setText(key);
//            key = QString(QString::fromLocal8Bit(this->bookcondition(booklist[i]->book_cond)));
//            ui->label_14->setText(key);
//            int condition = booklist[i]->isBorrowed;
//            char *s =this->bookcondition(condition);
//            key = QString(QString::fromLocal8Bit(s));
//            ui->label_15->setText(key);
            key = QString(QString::number(booklist[i]->borrow_num));
            ui->label_16->setText(key);

        }
        if(booklist.size()>i+1)
        {
            this->show2();
            key = QString(QString::number(i+2));
            ui->label_17->setText(key);
            string s1 = "正常",s2 = "破旧",s3 = "破损（需更换）";
            string s4= "信息错误",s5="未借出",s6="已借出";
            int book_cond = booklist[i+1]->book_cond;
            if(book_cond==0)
            {
                key = str2Qstr(UtfToGbk(s1.data()));
            }
            else if(book_cond==1)
            {
                key = str2Qstr(UtfToGbk(s2.data()));
            }
            else if(book_cond==2)
            {
                key = str2Qstr(UtfToGbk(s3.data()));
            }
            else
            {
                key = str2Qstr(UtfToGbk(s4.data()));
            }
            ui->label_18->setText(key);
            int condition = booklist[i+1]->isBorrowed;
            if(condition==-1)
            {
                key = str2Qstr(UtfToGbk(s5.data()));
            }
            else
            {
                key = str2Qstr(UtfToGbk(s6.data()));
            }
            ui->label_19->setText(key);
//            key = QString(QString::fromLocal8Bit(this->bookcondition(booklist[i+1]->book_cond)));
//            ui->label_18->setText(key);
//            key = QString(QString::fromLocal8Bit(this->bookisborrowed(booklist[i+1]->isBorrowed)));
//            ui->label_19->setText(key);
            key = QString(QString::number(booklist[i+1]->borrow_num));
            ui->label_20->setText(key);
        }
        if(booklist.size()>i+2)
        {
            this->show3();
            key = QString(QString::number(i+3));
            ui->label_21->setText(key);
            string s1 = "正常",s2 = "破旧",s3 = "破损（需更换）";
            string s4= "信息错误",s5="未借出",s6="已借出";
            int book_cond = booklist[i+2]->book_cond;
            if(book_cond==0)
            {
                key = str2Qstr(UtfToGbk(s1.data()));
            }
            else if(book_cond==1)
            {
                key = str2Qstr(UtfToGbk(s2.data()));
            }
            else if(book_cond==2)
            {
                key = str2Qstr(UtfToGbk(s3.data()));
            }
            else
            {
                key = str2Qstr(UtfToGbk(s4.data()));
            }
            ui->label_22->setText(key);
            int condition = booklist[i+2]->isBorrowed;
            if(condition==-1)
            {
                key = str2Qstr(UtfToGbk(s5.data()));
            }
            else
            {
                key = str2Qstr(UtfToGbk(s6.data()));
            }
            ui->label_23->setText(key);
//           key = QString(QString::fromLocal8Bit(this->bookcondition(booklist[i+2]->book_cond)));
//            ui->label_22->setText(key);
//            key = QString(QString::fromLocal8Bit(this->bookisborrowed(booklist[i+2]->isBorrowed)));
//            ui->label_23->setText(key);
            key = QString(QString::number(booklist[i+2]->borrow_num));
            ui->label_24->setText(key);
        }
        page = numofpage;
        ui->label_25->setText(QString::number(pagesize+1));
        ui->lineEdit->setText(QString::number(page+1));
        }
}

char* MenuToBooks::bookcondition(int condition)
{
    char *s;
    switch (condition)
    {
    case 0:
        strcpy(s,"正常");
        return s;
    case 1:
        strcpy(s,"破旧");
        return s;
    case 2:
        strcpy(s,"破损（需更换）");
        return s;
    default:
        strcpy(s,"信息错误");
        return s;
    }
}

char *MenuToBooks::bookisborrowed(int condition)
{
    char *s;
    if(condition==-1)
        strcpy(s,"未借出");
    else
        strcpy(s,"已借出");
    return s;
}


void MenuToBooks::clear()
{
    ui->label_13->clear();
    ui->label_13->hide();
    ui->label_14->clear();
    ui->label_14->hide();
    ui->label_15->clear();
    ui->label_15->hide();
    ui->label_16->clear();
    ui->label_16->hide();
    ui->label_17->clear();
    ui->label_17->hide();
    ui->label_18->clear();
    ui->label_18->hide();
    ui->label_19->clear();
    ui->label_19->hide();
    ui->label_20->clear();
    ui->label_20->hide();
    ui->label_21->clear();
    ui->label_21->hide();
    ui->label_22->clear();
    ui->label_22->hide();
    ui->label_23->clear();
    ui->label_23->hide();
    ui->label_24->clear();
    ui->label_24->hide();
    ui->pushButton->hide();
    ui->pushButton_2->hide();
    ui->pushButton_3->hide();
    ui->pushButton_4->hide();
    ui->pushButton_5->hide();
    ui->pushButton_6->hide();
    ui->pushButton_7->hide();
    ui->pushButton_8->hide();
    ui->pushButton_9->hide();
    ui->lineEdit->clear();
    ui->lineEdit->hide();
    ui->label_25->clear();
    ui->label_25->hide();
}

void MenuToBooks::show1()
{
    ui->label_13->show();
    ui->label_14->show();
    ui->label_15->show();
    ui->label_16->show();
    ui->pushButton->show();
    ui->pushButton_7->show();
}

void MenuToBooks::show2()
{
    ui->label_17->show();
    ui->label_18->show();
    ui->label_19->show();
    ui->label_20->show();
    ui->pushButton_2->show();
    ui->pushButton_8->show();
}

void MenuToBooks::show3()
{
    ui->label_21->show();
    ui->label_22->show();
    ui->label_23->show();
    ui->label_24->show();
    ui->pushButton_3->show();
    ui->pushButton_9->show();
}

void MenuToBooks::on_pushButton_4_clicked()
{
    if(page>0)
    {
        page--;
        this->show(bookList,page);
    }
    else if (page==0)
    {
        QMessageBox::warning(this,"警告","已经是第一页！");
    }
}

void MenuToBooks::on_pushButton_5_clicked()
{
    if(page<pagesize)
    {
        page++;
        this->show(bookList,page);
    }
    else if(page==pagesize)
    {
        QMessageBox::warning(this,"警告","已经是最后一页！");
    }
}

void MenuToBooks::on_pushButton_6_clicked()
{
    QString str = ui->lineEdit->text();
    int jumpofpage = str.toInt();
    if(jumpofpage>=1&&jumpofpage<=pagesize)
        this->show(bookList,jumpofpage-1);
    else
    {
        QMessageBox::warning(this,"警告","找不到该页！");
    }
}

void MenuToBooks::on_pushButton_7_clicked()
{
    int i = page_size*page;
    int reserving = reserve(stu->ID,bookList[i]->ID);
    if(reserving==0)
    {
        QMessageBox::warning(this,"提示","预约成功！");
        bookList = search_multiplebookReturn("notFuzzySearch","menu_id",menu->ID,bookDatPath);
        this->show(bookList,page);
    }
    else if(reserving==-1)
    {
        QMessageBox::warning(this,"提示","预约失败，书籍可直接借阅！");
    }
    else if(reserving==-2)
    {
        QMessageBox::warning(this,"提示","预约失败，书籍已被预约！");
    }
    else if(reserving==-3)
    {
        QMessageBox::warning(this,"提示","预约失败，预约已达上限！");
    }
}


void MenuToBooks::on_pushButton_8_clicked()
{
    int i = page_size*page+1;
    int reserving = reserve(stu->ID,bookList[i]->ID);
    if(reserving==0)
    {
        QMessageBox::warning(this,"提示","预约成功！");
        bookList = search_multiplebookReturn("notFuzzySearch","menu_id",menu->ID,bookDatPath);
        this->show(bookList,page);
    }
    else if(reserving==-1)
    {
        QMessageBox::warning(this,"提示","预约失败，书籍可直接借阅！");
    }
    else if(reserving==-2)
    {
        QMessageBox::warning(this,"提示","预约失败，书籍已被预约！");
    }
    else if(reserving==-3)
    {
        QMessageBox::warning(this,"提示","预约失败，预约已达上限！");
    }
}

void MenuToBooks::on_pushButton_9_clicked()
{
    int i = page_size*page+2;
    int reserving = reserve(stu->ID,bookList[i]->ID);
    if(reserving==0)
    {
        QMessageBox::warning(this,"提示","预约成功！");
        bookList = search_multiplebookReturn("notFuzzySearch","menu_id",menu->ID,bookDatPath);
        this->show(bookList,page);
    }
    else if(reserving==-1)
    {
        QMessageBox::warning(this,"提示","预约失败，书籍可直接借阅！");
    }
    else if(reserving==-2)
    {
        QMessageBox::warning(this,"提示","预约失败，书籍已被预约！");
    }
    else if(reserving==-3)
    {
        QMessageBox::warning(this,"提示","预约失败，预约已达上限！");
    }
}
